package com.alks.function.mapper.chengKong;

import com.alks.entity.data.entity.*;
import com.alks.function.data.dto.chengKong.assort.*;
import com.alks.function.data.request.chengKong.CommonQueryRequest;
import com.alks.function.data.request.chengKong.MaterialQueryRequest2;
import com.alks.function.data.request.chengKong.StyleBasicRequest;
import com.alks.function.data.request.chengKong.StyleNumberRequest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Mapper
public interface GeneralMaterialsMapper {

    /**
     * 根据分类代码查询
     * @param mtlNo
     * @return
     */
    @Select("select MTL_NO from YF_MATERIAL where MTL_TYPE=#{mtlNo}")
    Set<String> getMtlNoList(@Param("mtlNo") String mtlNo);

    /**
     * 通用材料建档
     * @param yfMaterial
     * @return
     */
    int AddGeneralMaterials(YfMaterial yfMaterial);

    /**
     * 材料查询列表
     * @param request
     * @return
     */
    List<YfMaterialDto> getMaterialQueryList(MaterialQueryRequest2 request);

    /**
     * 材料查询修改
     * @param yfMaterial
     * @return
     */
    int updateMaterial(YfMaterial yfMaterial);

    /**
     * 材料查询删除
     * @param mtlNo
     * @return
     */
    @Delete("delete from YF_MATERIAL where MTL_NO=#{mtlNo}")
    int delMaterialDel(String mtlNo);

    /**
     * 正式材料明细
     * @param request
     */
    List<YfBomMtlListDto> PublicMaterialsList(CommonQueryRequest request);

    /**
     * 正式材料明细修改
     * @param yfBomMtl
     * @return
     */
    @Update("update YF_BOM_MTL set " +
            "SAFETY_QTY=#{safetyQty},PUBLIC_FLAG=#{publicFlag},SYS_USER=#{sysUser},SYS_DATE=#{sysDate} " +
            "where MTL_NO=#{mtlNo}")
    int updatePublicMaterials(YfBomMtl yfBomMtl);

    /**
     * 查询型体是否存在
     * @param styleNo
     * @return
     */
    @Select("select STYLE_NO from YF_STYLE where STYLE_NO=#{styleNo} LIMIT 1")
    String getStyleNo(String styleNo);

    /**
     * 新增作业
     * @param yfStyle
     */
    @Insert("insert into YF_STYLE(STYLE_NO,TECH_PERSON,COMPANY_ID,SYS_USER,SYS_DATE,STYLE_NAME,SIZE_TYPE,BASE_SIZE,BASE_CUT_SIZE,UPPER_MTL,CUST_NO," +
            "CUST_STYLE,SEASON,SHOE_TYPE,SHOE_SEX,LAST_METHOD,CUST_BRAND,PREV_CUST_STYLE,SOLE_VENDOR,SOLE_MTL,SAMPLE_DESIGNER,MTL_CHECKER)" +
            "values(#{styleNo},#{techPerson},#{companyId},#{sysUser},#{sysDate},#{styleName},#{sizeType},#{baseSize},#{baseCutSize},#{upperMtl},#{custNo}," +
            "#{custStyle},#{season},#{shoeType},#{shoeSex},#{lastMethod},#{custBrand},#{prevCustStyle},#{soleVendor},#{soleMtl},#{sampleDesigner},#{mtlChecker}) ")
    void AddStyle(YfStyle yfStyle);

    /**
     * 查询号码信息
     * @param sizeType
     * @param companyId
     * @return
     */
    @Select("SELECT t2.SEQ_NO,t2.SIZE_NO from (SELECT * FROM PC_LOT_SIZE_SEQ_NOTICE where SIZE_NOTICE=#{sizeType} and COMPANY_ID=#{companyId}) t1 \n" +
            "LEFT JOIN PC_LOT_SIZE_SEQ t2 on t1.SIZE_CODE=t2.SIZE_NOTICE and t1.COMPANY_ID=t2.COMPANY_ID ORDER BY t2.SEQ_NO asc")
    List<PcLotSizeSeqDto> getSizeTypeByName(@Param("sizeType") String sizeType,
                                            @Param("companyId") String companyId);

    /**
     * 基本资料修改
     * @param yfStyle
     */
    @Update("update YF_STYLE set " +
            "STYLE_NAME=#{styleName},SIZE_TYPE=#{sizeType},BASE_SIZE=#{baseSize},BASE_CUT_SIZE=#{baseCutSize},UPPER_MTL=#{upperMtl}," +
            "CUST_NO=#{custNo},CUST_STYLE=#{custStyle},SEASON=#{season},SHOE_TYPE=#{shoeType},SHOE_SEX=#{shoeSex},LAST_METHOD=#{lastMethod}," +
            "CUST_BRAND=#{custBrand},PREV_CUST_STYLE=#{prevCustStyle},SOLE_VENDOR=#{soleVendor},SOLE_MTL=#{soleMtl},SAMPLE_DESIGNER=#{sampleDesigner}," +
            "MTL_CHECKER=#{mtlChecker},SYS_USER=#{sysUser},SYS_DATE=#{sysDate} where STYLE_NO=#{styleNo}")
    void updateStyle(YfStyle yfStyle);

    /**
     * 删除制具资料
     * @param styleNo
     * @return
     */
    @Delete("delete from YF_STYLE_TYPE where STYLE_NO=#{styleNo}")
    void delStyleType(String styleNo);

    /**
     * 新增制具资料
     * @param yfStyleType
     */
    @Insert("insert into YF_STYLE_TYPE(COMPANY_ID,STYLE_NO,TYPE_NO,LAST_MOLD,CUT_TOOL,SOLE_PART,MIDSOLE,INSOLE,ITEM_NAME,SEQ_NO1,SEQ_NO2,SIZE1,SIZE2)" +
            "values(#{companyId},#{styleNo},#{typeNo},#{lastMold},#{cutTool},#{solePart},#{midsole},#{insole},#{itemName},#{seqNo1},#{seqNo2},#{size1},#{size2})")
    void AddStyleType(YfStyleType yfStyleType);

    /**
     * 取号码插入
     * @param numberList 号码集合
     * @param companyId 公司
     * @param typeNo 制具序号
     */
    @Insert({
            "<script>",
            "insert into YF_STYLE_SIZE(STYLE_NO,SEQ_NO,SIZE_NO,CUT_SIZE,LAST_SIZE,SOLE_SIZE,INSOLE_SIZE,COMPANY_ID,MIDSOLE_SIZE,TYPE_NO)",
            "values ",
            "<foreach collection='numberList' item='item' separator=','>",
            "( #{styleNo},#{item.seqNo},#{item.sizeNo},#{item.cutSize},#{item.lastSize},#{item.soleSize},#{item.insoleSize},#{companyId}," +
                    "#{item.midsoleSize},#{typeNo})",
            "</foreach>",
            "</script>"
    })
    void addStyleNumber(@Param("numberList") List<StyleNumberRequest> numberList,
                        @Param("companyId") String companyId,
                        @Param("typeNo") BigDecimal typeNo,
                        @Param("styleNo") String styleNo);

    /**
     * 删除配码
     * @param styleNo
     */
    @Delete("DELETE from YF_STYLE_SIZE where STYLE_NO=#{styleNo}")
    void delStyleSize(@Param("styleNo") String styleNo);

    /**
     * 删除型体配色资料
     * @param styleNo
     * @return
     */
    @Delete("delete from YF_STYLE_COLOR_DETAIL where STYLE_NO=#{styleNo}")
    void delStyleColor(@Param("styleNo") String styleNo);

    /**
     * 添加型体配色资料
     * @param photoList
     */
    @Insert({
            "<script>",
            "insert into YF_STYLE_COLOR_DETAIL(COMPANY_ID,STYLE_NO,STYLE_COLOR,SEQ_NO,COLOR_CODE,ENG_STYLE_COLOR,SHORT_STYLE_COLOR,PHOTO_PATH,TASK_STATE,SEASON,SYS_USER,SYS_TIME)",
            "values ",
            "<foreach collection='photoList' item='item' separator=','>",
            "(#{item.companyId},#{item.styleNo},#{item.styleColor},#{item.seqNo},#{item.colorCode},#{item.engStyleColor},#{item.shortStyleColor},#{item.photoPath},#{item.taskState},#{item.season},#{item.sysUser},now())",
            "</foreach>",
            "</script>"
    })
    void AddStyleColorPhoto(@Param("photoList") List<YfStyleColorDetail> photoList);

    /**
     * 修改型体颜色备注
     * @param styleNo
     * @param styleColor
     * @param styleColorRemark
     */
    @Update("update YF_STYLE_COLOR_DETAIL set REMARK=#{styleColorRemark} where STYLE_NO=#{styleNo} and STYLE_COLOR=#{styleColor}")
    void updateStyleColorRemark(@Param("styleNo")String styleNo,
                                @Param("styleColor")String styleColor,
                                @Param("styleColorRemark")String styleColorRemark);

    /**
     * 查询基本资料与制具资料
     * @param request
     * @param userName
     */
    List<YfStyle2TypeDto> getStyleBasicList(@Param("request") StyleBasicRequest request,
                                            @Param("userName") String userName);

    /**
     * 制具资料
     * @param styleNo
     */
    SdStyleType getStyleType(@Param("styleNo") String styleNo);

    /**
     * 配码资料查询
     * @param styleNo
     * @param typeNo
     * @return
     */
    @Select("select SIZE_NO,CUT_SIZE,LAST_SIZE,SOLE_SIZE,INSOLE_SIZE,MIDSOLE_SIZE,SEQ_NO,COMPANY_ID from YF_STYLE_SIZE WHERE STYLE_NO=#{styleNo} AND TYPE_NO=#{typeNo} order by SEQ_NO asc")
    List<YfStyleSizeDto> getStyleSizeList(@Param("styleNo")String styleNo,
                                          @Param("typeNo")String typeNo);

    /**
     * 型体配色资料
     * @param styleNo
     * @return
     */
    @Select("select SEQ_NO,COLOR_CODE,STYLE_COLOR,ENG_STYLE_COLOR,SHORT_STYLE_COLOR,PHOTO_PATH,TASK_STATE,SEASON from " +
            "YF_STYLE_COLOR_DETAIL where STYLE_NO=#{styleNo} order by SEQ_NO asc")
    List<SdStyleColorDto> getStylePhotoList(String styleNo);

    /**
     * 删除型体基本资料
     * @param styleNo
     */
    @Delete("delete from YF_STYLE where STYLE_NO=#{styleNo} ")
    void delStyleBasic(@Param("styleNo") String styleNo);

    /**
     * 制单员修改
     * @param yfStyle
     */
    @Update("update YF_STYLE set TECH_PERSON=#{techPerson},SYS_USER=#{sysUser},SYS_DATE=#{sysDate} where STYLE_NO=#{styleNo}")
    void updateStyle2TechPerson(YfStyle yfStyle);

    /**
     * 检查是否检核
     * @param styleNo
     * @return
     */
    @Select("SELECT CFM_FLAG from YF_STYLE where STYLE_NO=#{styleNo} limit 1")
    String getCheckUp(@Param("styleNo") String styleNo);

    /**
     * 修改型体检验状态
     * @param styleNoList
     * @param date
     * @param userName
     * @param cfmFlag
     */
    @Update({"<script>",
            "UPDATE YF_STYLE SET CFM_FLAG=#{cfmFlag}, CFM_USER=#{userName}, CFM_DATE=#{date} WHERE STYLE_NO IN " +
            "<foreach collection='styleNoList' item='styleNoList' separator=',' open='(' close=')'>" +
            "#{styleNoList}" +
            "</foreach>",
            "</script>"
    })
    void updateStyle2Inspect(@Param("styleNoList") List<String> styleNoList,
                             @Param("date") Date date,
                             @Param("userName") String userName,
                             @Param("cfmFlag") String cfmFlag);

    /**
     * 取号码
     * @param sizeType
     * @param companyId
     * @param sizeCode
     * @return
     */
    @Select("SELECT t2.SEQ_NO,t2.SIZE_NO from (SELECT * FROM PC_LOT_SIZE_SEQ_NOTICE where SIZE_NOTICE=#{sizeType} and COMPANY_ID=#{companyId}" +
            "and SIZE_CODE=#{sizeCode})t1 \n" +
            "LEFT JOIN PC_LOT_SIZE_SEQ t2 on t1.SIZE_CODE=t2.SIZE_NOTICE and t1.COMPANY_ID=t2.COMPANY_ID ORDER BY t2.SEQ_NO asc")
    List<PcLotSizeSeqDto> getSizeTypeByName2(@Param("sizeType") String sizeType,
                                             @Param("companyId") String companyId,
                                             @Param("sizeCode")String sizeCode);

    /**
     * 查询是否被研发bom资料引用
     * @param styleNo
     * @return
     */
    @Select("SELECT count(1) from YF_BOM_HEAD_NEW2 where STYLE_NO=#{styleNo}")
    Integer getBomIsExist(String styleNo);

    /**
     * 添加型体配色资料
     * @param styleColorDetail
     */
    @Insert({
            "insert into YF_STYLE_COLOR_DETAIL(COMPANY_ID,STYLE_NO,STYLE_COLOR,SEQ_NO,COLOR_CODE,ENG_STYLE_COLOR,SHORT_STYLE_COLOR,PHOTO_PATH,TASK_STATE,SEASON,SYS_USER,SYS_TIME)",
            "values ",
            "(#{companyId},#{styleNo},#{styleColor},#{seqNo},#{colorCode},#{engStyleColor},#{shortStyleColor},#{photoPath},#{taskState},#{season},#{sysUser},now())",
    })
    int addYfStyleColorDetail(YfStyleColorDetail styleColorDetail);

    /**
     * 删除型体配色资料
     * @param styleNo
     * @param styleColor
     * @return
     */
    @Delete("delete from YF_STYLE_COLOR_DETAIL where STYLE_NO=#{styleNo} and STYLE_COLOR=#{styleColor}")
    int delYfStyleColorDetail(@Param("styleNo") String styleNo, @Param("styleColor") String styleColor);

    /**
     * 修改型体配色资料
     * @param styleColorDetail
     * @return
     */
    @Update("update YF_STYLE_COLOR_DETAIL set SEQ_NO=#{seqNo},COLOR_CODE=#{colorCode},ENG_STYLE_COLOR=#{engStyleColor}," +
            "SHORT_STYLE_COLOR=#{shortStyleColor},PHOTO_PATH=#{photoPath},REMARK=#{remark},TASK_STATE=#{taskState},SEASON=#{season}," +
            "UPDATE_USER=#{updateUser},UPDATE_TIME=now() where STYLE_NO=#{styleNo} and STYLE_COLOR=#{styleColor}")
    int updateByStyleColorDetail(YfStyleColorDetail styleColorDetail);
}
